// const dayjs = require('dayjs')
const dayjs = require('../../miniprogram_npm/dayjs/index')

console.log(dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss'))

Page({
  data: {
    films: [], // 存放电影列表数据
    total: 0, // 数据总条数
    hasMore: true, // 是否还有更多
    lineLeft: '24%', // tab 小横线的left的值
  },

  pageNum: 1,
  pageSize: 10,
  type: 1,
  cityId: 440300,

  onLoad() {
    console.log('Films - onLoad')
    // this.getFilms()
    wx.startPullDownRefresh()
  },

  onShow() {
    console.log('Films - onShow')
  },

  /**
   * 触底
   */
  onReachBottom() {
    // 判断是否还需要请求下一页
    // if (this.data.films.length >= this.data.total) {
    //   return
    // }
    if (!this.data.hasMore) {
      return
    }
    this.pageNum++
    this.getFilms()
  },

  /**
   * 下拉
   */
  onPullDownRefresh() {
    this.pageNum = 1
    this.getFilms()
  },

  /**
   * 获取电影列表数据
   */
  getFilms() {
    wx.request({
      url: 'https://m.maizuo.com/gateway',
      header: {
        "X-Client-Info": '{"a":"3000","ch":"1002","v":"5.2.1","e":"16635571112332257436041217","bc":"150200"}',
        "X-Host": "mall.film-ticket.film.list"
      },
      data: {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        type: this.type,
        cityId: this.cityId,
        k: 5247765,
      },
      success: ({data}) => {
        // console.log(data, this)
        if (data.status === 0) {
          const totalPage = Math.ceil(data.data.total / this.pageSize) // 总页数
          const hasMore = this.pageNum < totalPage
          const films = data.data.films.map(item => ({
            ...item,
            actorsText: this.formatActors(item.actors)
          }))

          this.setData({
            films: this.pageNum === 1 ? films : [...this.data.films, ...films],
            total: data.data.total,
            hasMore
          })
        }
      },
      complete: () => {
        // wx.showToast({
        //   title: 'complete',
        // })
        wx.stopPullDownRefresh()
      }
    })
  },

  handleTabTap(event) {
    const { index, type } = event.target.dataset
    let left = '24%'
    if (+index === 1) {
      left = '64%'
    }

    this.setData({
      lineLeft: left
    })

    this.type = type // 设置接口请求的 type 字段
    wx.startPullDownRefresh()
  },

  /**
   * 点击电影，跳转到详情页面
   */
  handleJump(event) {
    // 请注意，currentTarget 与 target 的区别
    const { name, id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/films-detail/films-detail?name=${name}&id=${id}`,
      // 可以通过 success 的参数中有个 eventChannel 获取到 与跳转到的页面 this.getOpenerEventChannel() 
      success: (res) => {
        console.log('Films - navigateTo - success', res)
        // 使用 res.eventChannel 做事件的绑定
        res.eventChannel.on('changeName', (data) => {
          // debugger
          console.log('changeName 的回调', data)
          // // 对当前 films 数据直接做修改 [{filmId: , name}]
          // const index = this.data.films.findIndex(item => item.filmId === +data.id)
          // const newFilms = [...this.data.films]
          // newFilms[index].name = data.name
          // this.setData({
          //   films: newFilms
          // })
          this.fnfn(data)
        })
      }
    })
  },

  fnfn(data) {
    console.log('fnfn', data)
    // 对当前 films 数据直接做修改 [{filmId: , name}]
    const index = this.data.films.findIndex(item => item.filmId === +data.id)
    const newFilms = [...this.data.films]
    newFilms[index].name = data.name
    this.setData({
      films: newFilms
    })
  },

  formatActors(actors) {
    return actors.map(item => item.name).join(' ')
  }
})